//
//  ZCXPerPictureViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXPerPictureViewController.h"
#import "ZFHYNetWork.h"
#import "ZCXPetPictureCollectionViewCell.h"
#import "ZCXPetPictureModel.h"
#import "FlowLayout.h"
#import "ZCXFirstActivityDetialTableViewCell.h"
#import "ZCXPetpictureDetialViewController.h"
#import "FileManage.h"
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

@interface ZCXPerPictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,CJCollectionViewFlowlayout >
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *petArray;
@property (nonatomic, copy) NSString *path;

@end

@implementation ZCXPerPictureViewController
- (void)dealloc{
    [_collectionView release];
    [_petArray release];
    [_path release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.petArray = [NSMutableArray array];
    
    self.path = [FileManage createFile:@"picture.plist"];
    NSDictionary *dic = [FileManage readFile:self.path];
    NSArray *array = [dic objectForKey:@"data"];
    for (NSDictionary *dicc in array) {
        ZCXPetPictureModel *petPic = [ZCXPetPictureModel ZCXPetPictureModelWithDictionay:dicc];
        [self.petArray addObject:petPic];
    }
    
    [self loadDataWithString:@"http://ic.snssdk.com/2/image/recent/?tag=meng&max_behot_time=1447607961&count=0&iid=3254973844"];
    FlowLayout *flowLayout = [[FlowLayout alloc] init];
    flowLayout.delegate = self;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //Cell的注册
    [self.collectionView registerClass:[ZCXPetPictureCollectionViewCell class] forCellWithReuseIdentifier:@"zcxpetpicturecollectioncell"];
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor grayColor];
    [flowLayout release];
    [_collectionView release];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.petArray.count;
}
- (void)loadDataWithString:(NSString *)urlString{
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        [FileManage writeFile:dic andFilePath:self.path];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dicc in array) {
            ZCXPetPictureModel *petPic = [ZCXPetPictureModel ZCXPetPictureModelWithDictionay:dicc];
            [self.petArray addObject:petPic];
        }
        [self.collectionView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZCXPetPictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zcxpetpicturecollectioncell" forIndexPath:indexPath];
    cell.petModel = self.petArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1 * WIDTH;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1 * WIDTH;
}
- (CGSize)collectionview:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtItemIndexPath:(NSIndexPath *)indexPath{
    
    
    CGFloat hh = [[self.petArray[indexPath.row] middle_height]floatValue] / [[self.petArray[indexPath.row] middle_width] floatValue];
    CGFloat heigth = 0;
    heigth =  hh * self.view.frame.size.width / 2 ;
    CGFloat width = self.view.frame.size.width / 2 - 10 * WIDTH;
    CGFloat labelHeigth = [ZCXFirstActivityDetialTableViewCell heigthOfmessageLabel:[self.petArray[indexPath.row] Description] andFont:[UIFont systemFontOfSize:15 * WIDTH] andWidth:width - 10 * WIDTH];
    return CGSizeMake(width, heigth + labelHeigth);
    
    
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5 * WIDTH, 5 * WIDTH, 5 * WIDTH, 5 * WIDTH);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCXPetpictureDetialViewController *petPictureDetial = [[ZCXPetpictureDetialViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    
    UIBarButtonItem *backIetm = [[UIBarButtonItem alloc] init];
    backIetm.title =@"返回";
    self.navigationItem.backBarButtonItem = backIetm;
    petPictureDetial.urlString = [self.petArray[indexPath.row] group_id];
    petPictureDetial.count = [self.petArray[indexPath.row] middle_url];
    petPictureDetial.heigth = [self.petArray[indexPath.row] middle_height];
    petPictureDetial.width = [self.petArray[indexPath.row] middle_width];
    petPictureDetial.feel = [self.petArray[indexPath.row] Description];
    [self.navigationController pushViewController:petPictureDetial animated:YES];
    [petPictureDetial release];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
